package com.tte.common.utils.office;

import com.aspose.words.*;

import java.util.Iterator;

public class WordByAspose {
    public static String parentPath = "D:\\temp";
    public static String tempFilePath = "D:\\temp\\模板.docx";
    public static String newFilePath = "D:\\temp\\模板1.docx";
    public static String newFilePath2 = "D:\\temp\\模板2.docx";

    public static void main(String[] args) throws Exception {

        copyForTemplate(tempFilePath, newFilePath2, 300);
//        createFilePage(5);
    }

    /**
     * 根据模板追加
     * @param tempFilePath   模板路径
     * @param targetFilePath 生成文件路径
     * @param copies         份数
     * @return
     * @Author: DongWH
     * @Date: 2024/4/24 14:08
     */
    public static void createFilePage(String tempFilePath, String targetFilePath, int copies) {
        try {
            Document doc = new Document(tempFilePath);
            Document ddd = new Document();
            for (int y = 0; y < copies; y++) {
                ddd.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
            }
            ddd.save(targetFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description: 根据模板生成多份文件
     * @param tempFilePath   模板路径
     * @param targetFilePath 生成文件路径
     * @param copies         份数
     * @return
     * @Author: DongWH
     * @Date: 2024/4/24 14:08
     */
    public static void copyForTemplate(String tempFilePath, String targetFilePath,int copies) throws Exception {
        Document wordCopy = new Document(tempFilePath);
        Document word = new Document(tempFilePath);
        for (int i = 0; i < copies - 1; i++) {
            appendDoc(word, wordCopy, false);
        }
        word.save(targetFilePath);
    }

    public static void appendDoc(Document dstDoc, Document srcDoc, boolean includeSection) throws Exception {
        if (includeSection) {
            Iterator<Section> var3 = srcDoc.getSections().iterator();
            while (var3.hasNext()) {
                Section srcSection = (Section) var3.next();
                Node dstNode = dstDoc.importNode(srcSection, true, 0);
                dstDoc.appendChild(dstNode);
            }
        } else {
            Node node = dstDoc.getLastSection().getBody().getLastParagraph();
            if (node == null) {
                node = new Paragraph(srcDoc);
                dstDoc.getLastSection().getBody().appendChild(node);
            }

            if (node.getNodeType() != 8 & node.getNodeType() != 5) {
                throw new Exception("Use appendDoc(dstDoc, srcDoc, true) instead of appendDoc(dstDoc, srcDoc, false)");
            }

            insertDocumentAfterNode(node, dstDoc, srcDoc);
        }
    }


    public static void insertDocumentAfterNode(Node insertAfterNode, Document mainDoc, Document srcDoc)
            throws Exception {
        if (insertAfterNode.getNodeType() != 8 & insertAfterNode.getNodeType() != 5) {
            throw new Exception("The destination node should be either a paragraph or table.");
        } else {
            CompositeNode dstStory = insertAfterNode.getParentNode();

            while (null != srcDoc.getLastSection().getBody().getLastParagraph()
                    && !srcDoc.getLastSection().getBody().getLastParagraph().hasChildNodes()) {
                srcDoc.getLastSection().getBody().getLastParagraph().remove();
            }

            NodeImporter importer = new NodeImporter(srcDoc, mainDoc, 1);
            int sectCount = srcDoc.getSections().getCount();

            for (int sectIndex = 0; sectIndex < sectCount; ++sectIndex) {
                Section srcSection = srcDoc.getSections().get(sectIndex);
                int nodeCount = srcSection.getBody().getChildNodes().getCount();

                for (int nodeIndex = 0; nodeIndex < nodeCount; ++nodeIndex) {
                    Node srcNode = srcSection.getBody().getChildNodes().get(nodeIndex);
                    Node newNode = importer.importNode(srcNode, true);
                    dstStory.insertAfter(newNode, insertAfterNode);
                    insertAfterNode = newNode;
                }
            }

        }
    }

}
